import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/model/home_list.dart';
import 'package:flutter_elf/network/service/home_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MainPage extends BaseWidget {
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return MainPageState();
  }
}

class MainPageState extends BaseWidgetState<MainPage> {
  List<HomeListData> datas = List();
  RefreshController _refreshController = RefreshController(initialRefresh: true);
  @override
  void onCreate() {
    // TODO: implement onCreate
    super.onCreate();
  }

  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return Column(children: <Widget>[
      CachedNetworkImage(
        imageUrl:
            'http://attach.bbs.miui.com/forum/month_1012/10120514509c7244b23f4a2fa5.jpg',
        height: 250,
        width: double.infinity,
      ),
      Container(
        padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
        child: Row(
          children: <Widget>[
            Text(
              '最新表情',
              style: TextStyle(fontSize: 12, color: Colors.grey),
            ),
            Expanded(
              child: SizedBox(),
            ),
            Text(
              '更多',
              style: TextStyle(fontSize: 12, color: Colors.black),
            )
          ],
        ),
      ),

      Expanded(
        child: SmartRefresher(
          controller: _refreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: getData,
          onLoading: _onLoadMore,
          header: WaterDropHeader(),
          child: GridView.builder(
            physics: ClampingScrollPhysics(),
            semanticChildCount: 4,
            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
              //单个子Widget的水平最大宽度
              maxCrossAxisExtent: px(300),
              //水平单个子Widget之间间距
              mainAxisSpacing: px(10),
              //垂直单个子Widget之间间距
              crossAxisSpacing: 5,
            ),
            itemCount: datas.length,
            itemBuilder: (BuildContext context, int index) {
              return buildGridViewItem(datas[index]);
            },
          ),
        ),
      ),

    ]);
  }

  buildGridViewItem(HomeListData data){
    return Column(
      children: <Widget>[
        CachedNetworkImage(
          fit: BoxFit.fitHeight,
          imageUrl: data.gifPath,
          height: px(200),
          width: px(200),
        ),
        Text(data.name,style: TextStyle(fontSize: 14,color: Colors.black),maxLines: 1,)
      ],
    );
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
//    return AppBarCreator.create(context, '首页');
    return null;
  }

  int _page = 1;
  int _pageSize = 30;
  getData() async {
    _page = 1;
    datas.clear();
    Map<String,dynamic> params = Map();
    params['pageNum'] = _page;
    params['pageSize'] = _pageSize;
    HomeListResponse response =
    await HomeService.homeList(params,state: this);
    globalPrint('请求到的数据是' + response.toString());
    if (response?.data?.isNotEmpty ?? false) {
      _page++;
      setState(() {
        datas.addAll(response?.data);
        globalPrint('一共多少条数据=== 》 ' + datas.length.toString());
      });
      _refreshController.refreshCompleted();
    }
  }
  _onLoadMore() async {
    Map<String,dynamic> params = Map();
    params['pageNum'] = _page;
    params['pageSize'] = _pageSize;
    HomeListResponse response = await HomeService.homeList(params,state: this);

    if (response?.data?.isNotEmpty ?? false) {
      setState(() {
        _page++;
        datas.addAll(response?.data);
      });
          _refreshController.loadComplete();
    }
  }
}
